
package com.yesep.learn.mqtt.server.broker.handler.protocol;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yesep.learn.mqtt.protocol.IPubAck;
import com.yesep.learn.mqtt.server.common.Constants;
import com.yesep.learn.mqtt.server.common.message.IDupPublishMessageStoreService;
import com.yesep.learn.mqtt.server.common.message.IMessageIdService;
import com.yesep.learn.mqtt.util.ChannelUtil;

import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader;
import io.netty.handler.codec.mqtt.MqttPubAckMessage;
import io.netty.util.AttributeKey;

/**
   *   消息编号：4
 * @Description: 发布应答
 */
@Service
public class PubAck implements IPubAck {

	private static final Logger LOGGER = LoggerFactory.getLogger(PubAck.class);
	@Autowired
	private IMessageIdService messageIdService;
	@Autowired
	private IDupPublishMessageStoreService dupPublishMessageStoreService;

	public void process(Channel channel, MqttPubAckMessage msg) {
		MqttMessageIdVariableHeader variableHeader = msg.variableHeader();
		int messageId = variableHeader.messageId();
		String clientId = ChannelUtil.getClientId(channel);
		LOGGER.debug("PUBACK - clientId: {}, messageId: {}", clientId, messageId);
		dupPublishMessageStoreService.remove(clientId, messageId);
		messageIdService.releaseMessageId(messageId);
	}

}
